<template>
  <div class="mod-appointment">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="searchForm.userId" placeholder="客户id" clearable/>
      </el-form-item>
      <el-form-item>
        <el-dict code="discipline" v-model="searchForm.discipline"></el-dict>
      </el-form-item>
      <el-form-item>
        <span class="demonstration">预约创建时间</span>
        <el-date-picker
          v-model="searchForm.createTime"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <span class="demonstration">客户预约到院时间</span>
        <el-date-picker
          v-model="searchForm.appointmentTime"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="pageIndex = 1;getDataList()">查询</el-button>
        <el-button v-if="isAuth('mld:appointment:save')" type="primary" @click="editHandle()">新增</el-button>
        <el-button v-if="isAuth('mld:appointment:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table border :data="dataList" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"/>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间"/>
      <el-table-column prop="updateTime" header-align="center" align="center" label="更新时间"/>
      <el-table-column prop="appointmentId" header-align="center" align="center" label="预约记录id"/>
      <el-table-column prop="userId" header-align="center" align="center" label="客户id"/>
      <el-table-column prop="appointmentTime" header-align="center" align="center" label="客户预约到院时间"/>
      <el-table-column prop="arrived" header-align="center" align="center" label="是否到院标志">
        <template v-slot="scope">
          <span>{{transDict('select',scope.row.arrived)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="medicalTreatId" header-align="center" align="center" label="就诊id"/>
      <el-table-column prop="arrivedTime" header-align="center" align="center" label="来院时间"/>
      <el-table-column prop="appointmentEmployeeId" header-align="center" align="center" label="预约员工">
        <template v-slot="scope">
          <span>{{transUser(scope.row.appointmentEmployeeId)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="department" header-align="center" align="center" label="预约部门"/>
      <el-table-column prop="discipline" header-align="center" align="center" label="咨询项目">
        <template v-slot="scope">
          <span>{{transDict('discipline',scope.row.discipline)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="channel" header-align="center" align="center" label="预约渠道">
        <template v-slot="scope">
          <span>{{transDict('channel',scope.row.channel)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="gift" header-align="center" align="center" label="礼品类型">
        <template v-slot="scope">
          <span>{{transDict('gift',scope.row.gift)}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="weixinCustomerSource" header-align="center" align="center" label="微信开发渠道来源">
        <template v-slot="scope">
          <span>{{transDict('channel',scope.row.weixinCustomerSource)}}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('mld:appointment:info')" type="text" size="small" @click="showDetails(scope.row.id)">查看</el-button>
          <el-button v-if="isAuth('mld:appointment:update')" type="text" size="small" @click="editHandle(scope.row.id)">修改</el-button>
          <el-button v-if="isAuth('mld:appointment:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <appointment-edit v-if="editVisible" ref="appointmentEdit" @refreshDataList="getDataList"/>
  </div>
</template>

<script>
  import appointmentEdit from './appointment-edit'
  export default {
    data () {
      return {
        searchForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListSelections: [],
        editVisible: false,
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        }
      }
    },
    components: {
      appointmentEdit
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.$http({
          url: '/mld/appointment/list',
          method: 'get',
          params: {
            page: this.pageIndex,
            limit: this.pageSize,
            createTimeStart: this.searchForm.createTime ? this.searchForm.createTime[0] : null,
            createTimeEnd: this.searchForm.createTime ? this.searchForm.createTime[1] : null,
            appointmentTimeStart: this.searchForm.appointmentTime ? this.searchForm.appointmentTime[0] : null,
            appointmentTimeEnd: this.searchForm.appointmentTime ? this.searchForm.appointmentTime[1] : null,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 查看详情
      showDetails (id) {
        this.editVisible = true
        this.$nextTick(() => {
          this.$refs.appointmentEdit.init(id, true)
        })
      },
      // 新增 / 修改
      editHandle (id) {
        this.editVisible = true
        this.$nextTick(() => {
          this.$refs.appointmentEdit.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        let ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[删除]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: '/mld/appointment/delete',
            method: 'post',
            data: ids
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({ message: '操作成功', type: 'success', duration: 1500 })
              this.getDataList()
            }
          })
        })
      }
    }
  }
</script>
